package com.ds.bintree.LeetCode;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @description: TODO
 * @author  Dianzhi Han
 * @date 2022/9/14 17:15
 * @version 1.0
 */
public class Num222_CountNodes {
    public int countNodes(TreeNode root) {
        Deque<TreeNode> queue = new LinkedList<>();
        int size = 0;
        if(root==null){
            return 0;
        }
        queue.offer(root);
        size++;
        while (!queue.isEmpty()){
            TreeNode temp=queue.poll();
            if (temp!=null){
                queue.offer(temp.left);
                if (temp.left!=null){
                    size++;
                }
                queue.offer(temp.right);
                if (temp.right!=null){
                    size++;
                }
            }
        }
        return size;
    }
}
